import express, { Express, Request, Response, Application } from 'express'

import dotenv from 'dotenv'
import { testConnection } from './db/connection'
import routes from './config/routes'
import errorHandler from './middlewares/errorHandler'
import { randomBytes } from 'crypto'
import { httpLogger } from './middlewares/httpLogger'

//For env File

dotenv.config()

const app: Application = express()
app.use(express.json())
// 注册HTTP请求日志中间件
app.use(httpLogger)

const port = process.env.PORT || 8000

app.use(routes)

// 错误中间件
app.use(errorHandler)

app.listen(port, async () => {
  //测试数据库连接
  await testConnection()
  console.log(`Server is Fire at http: //localhost:${port}`)
})
